{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. 并发编程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.1 并发编程之 Futures\n",
    "\n",
    "- 单线程与多线程性能比较\n",
    "- "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://baike.baidu.com/item/Python: 323955\n",
      "https://baike.baidu.com/item/Java: 2497\n",
      "https://baike.baidu.com/item/PHP: 2496\n",
      "https://baike.baidu.com/item/Node.js: 2500\n",
      "https://baike.baidu.com/item/The_C_Programming_Language: 2519\n",
      "https://baike.baidu.com/item/Go_(programming_language): 2518\n",
      "https://baike.baidu.com/item/The_C_Programming_Language: 2519\n",
      "https://baike.baidu.com/item/Go_(programming_language): 2518\n",
      "https://baike.baidu.com/item/The_C_Programming_Language: 5169\n",
      "https://baike.baidu.com/item/Go_(programming_language): 2518\n",
      "https://baike.baidu.com/item/The_C_Programming_Language: 2519\n",
      "https://baike.baidu.com/item/Go_(programming_language): 2518\n",
      "https://baike.baidu.com/item/The_C_Programming_Language: 2519\n",
      "https://baike.baidu.com/item/Go_(programming_language): 2518\n",
      "Wall time: 1.5486448999999993 seconds\n",
      "CPU times: total: 62.5 ms\n",
      "Wall time: 1.55 s\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import time\n",
    "\n",
    "def download_one(url):\n",
    "    response = requests.get(url)\n",
    "    print('{}: {}'.format(url, len(response.content)))\n",
    "\n",
    "def download_all(sites):\n",
    "    for site in sites:\n",
    "        download_one(site)\n",
    "\n",
    "def main():\n",
    "    sites = [ 'https://baike.baidu.com/item/Python', \n",
    "            'https://baike.baidu.com/item/Java',\n",
    "            'https://baike.baidu.com/item/PHP', \n",
    "            'https://baike.baidu.com/item/Node.js',         \n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "              ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Wall time: {} seconds'.format(end_time - start_time))\n",
    "\n",
    "%time main()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.2 多线程版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0.2520825999999943 seconds\n",
      "CPU times: total: 62.5 ms\n",
      "Wall time: 253 ms\n"
     ]
    }
   ],
   "source": [
    "import  concurrent.futures\n",
    "import requests\n",
    "import threading\n",
    "import time\n",
    "\n",
    "def download_one(url):\n",
    "    response = requests.get(url)\n",
    "    print('{}: {}'.format(url, len(response.content)))\n",
    "\n",
    "def download_all(sites):\n",
    "    # with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "    #     executor.map(download_one, sites)\n",
    "    \n",
    "    with concurrent.futures.ProcessPoolExecutor() as executor:\n",
    "        executor.map(download_one, sites)\n",
    "\n",
    "def main():\n",
    "    sites = [ 'https://baike.baidu.com/item/Python', \n",
    "            'https://baike.baidu.com/item/Java',\n",
    "            'https://baike.baidu.com/item/PHP', \n",
    "            'https://baike.baidu.com/item/Node.js',         \n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',  \n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language'\n",
    "            ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Wall time: {} seconds'.format(end_time - start_time))\n",
    "\n",
    "%time main()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.3 到底什么是  Futures ？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read 2499 from https://baike.baidu.com/item/PythonRead 2500 from https://baike.baidu.com/item/Node.js\n",
      "\n",
      "Read 2496 from https://baike.baidu.com/item/PHP\n",
      "Read 2497 from https://baike.baidu.com/item/Java\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Read 2518 from https://baike.baidu.com/item/Go_(programming_language)\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Read 5169 from https://baike.baidu.com/item/Go_(programming_language)\n",
      "Read 2518 from https://baike.baidu.com/item/Go_(programming_language)\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Read 2518 from https://baike.baidu.com/item/Go_(programming_language)\n",
      "Read 2518 from https://baike.baidu.com/item/Go_(programming_language)\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Read 2519 from https://baike.baidu.com/item/The_C_Programming_Language\n",
      "Download 15 sites in 0.479715400000714 seconds\n",
      "CPU times: total: 93.8 ms\n",
      "Wall time: 480 ms\n"
     ]
    }
   ],
   "source": [
    "import concurrent.futures\n",
    "import requests\n",
    "import time\n",
    "\n",
    "def download_one(url):\n",
    "    resp = requests.get(url)\n",
    "    print('Read {} from {}'.format(len(resp.content), url))\n",
    "\n",
    "def download_all(sites):\n",
    "    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "        to_do = []\n",
    "        for site in sites:\n",
    "            future = executor.submit(download_one, site)\n",
    "            to_do.append(future)\n",
    "            \n",
    "        for future in concurrent.futures.as_completed(to_do):\n",
    "            future.result()\n",
    "def main():\n",
    "    sites = [ 'https://baike.baidu.com/item/Python', \n",
    "            'https://baike.baidu.com/item/Java',\n",
    "            'https://baike.baidu.com/item/PHP', \n",
    "            'https://baike.baidu.com/item/Node.js',         \n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',  \n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language',\n",
    "            'https://baike.baidu.com/item/Go_(programming_language)',\n",
    "            'https://baike.baidu.com/item/The_C_Programming_Language'\n",
    "            ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "\n",
    "%time main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
